package app.core.ar;

import com.gitee.feizns.quickstart.ddl.annotation.Comment;
import com.gitee.feizns.quickstart.mybatis.flex.jpa.TreePojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import java.util.Objects;

/**
 * 树表公共操作
 * @author feizns
 * @since 2023/06/04
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
@MappedSuperclass
public abstract class ArTree<T extends ArTree<T>> extends Ar<T> implements TreePojo<T, Long> {

    /**
     * 根编号
     */
    public static final Long ROOT_ID = 0L;

    /**
     * 父编号（默认为0）
     */
    @Comment("父编号（默认为0）")
    @Column(columnDefinition = "BIGINT UNSIGNED DEFAULT 0")
    protected Long parentId;

    /**
     * 根编号
     * @return {@link Long }
     */
    @Override
    public Long rootId() {
        return ROOT_ID;
    }

    /**
     * 是否是根
     * @param parentId 父编号
     * @return boolean
     */
    public static boolean isRoot(Long parentId) {
        return Objects.equals(parentId, 0L);
    }

}
